# -*- coding: utf-8 -*-
"""
xfree.cli.translations
~~~~~~~~~~~~~~~~~~~~~~~~

这个模块包含所有与翻译相关的命令。

:copyright: (c) 2016 by the xfree Team.
:license: BSD，查看 LICENSE 获取更多详细信息。
"""

import click

from xfree.cli.main import xfree
from xfree.cli.utils import validate_plugin
from xfree.utils.translations import (
    add_plugin_translations,
    add_translations,
    compile_plugin_translations,
    compile_translations,
    update_plugin_translations,
    update_translations,
)


@xfree.group()
def translations():
    """翻译命令子组。"""
    pass


@translations.command("new")
@click.option(
    "--plugin", "-p", type=click.STRING, help="为一个插件添加新的语言。"
)
@click.argument("lang")
def new_translation(lang, plugin):
    """为翻译添加一种新语言。“lang”是语言的代码，例如“de_AT”。"""
    if plugin:
        # 验证插件是否存在
        validate_plugin(plugin)
        click.secho(
            "[+] 为插件 {} 添加新语言 {}...".format(plugin, lang),
            fg="cyan",
        )
        # 为插件添加新语言翻译
        add_plugin_translations(plugin, lang)
    else:
        click.secho("[+] 添加新语言 {}...".format(lang), fg="cyan")
        # 添加新语言翻译
        add_translations(lang)


@translations.command("update")
@click.option(
    "is_all",
    "--all",
    "-a",
    default=False,
    is_flag=True,
    help="也更新插件的翻译。",
)
@click.option(
    "--plugin",
    "-p",
    type=click.STRING,
    help="更新给定插件的语言。",
)
def update_translation(is_all, plugin):
    """更新所有翻译。"""
    if plugin is not None:
        validate_plugin(plugin)
        click.secho(
            "[+] 为插件 {} 更新语言文件...".format(plugin), fg="cyan"
        )
        update_plugin_translations(plugin)
    else:
        click.secho("[+] 更新语言文件...", fg="cyan")
        update_translations(include_plugins=is_all)


@translations.command("compile")
@click.option(
    "is_all",
    "--all",
    "-a",
    default=False,
    is_flag=True,
    help="也编译插件的翻译。",
)
@click.option(
    "--plugin",
    "-p",
    type=click.STRING,
    help="编译给定插件的翻译。",
)
def compile_translation(is_all, plugin):
    """编译翻译。"""
    if plugin is not None:
        validate_plugin(plugin)
        click.secho(
            "[+] 为插件 {} 编译语言文件...".format(plugin), fg="cyan"
        )
        compile_plugin_translations(plugin)
    else:
        click.secho("[+] 编译语言文件...", fg="cyan")
        compile_translations(include_plugins=is_all)